package com.aboverock.integrations.fileserver.web;

import com.aboverock.integrations.fileserver.domain.File;
import com.aboverock.integrations.fileserver.service.FileService;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 文件上传下载接口的抽象基类, 可以直接继承使用
 *
 * 如下注解为示例：
 * 注解 CrossOrigin(origins = "*", maxAge = 3600) // 允许所有域名访问
 * 注解 Controller
 * 注解 RequestMapping("/fileServer")
 * <p>
 *
 * @author RockerWang
 * @date Created in 2021/12/2 23:36
 */
@RestController
@RequestMapping("/fileServer")
public class BaseFileServerController {

    @Autowired
    private FileService fileService;

    /**
     * 分页查询文件列表.
     *
     * @param current the current
     * @param pageSize the page size
     * @return the list
     */
    @ApiOperation("QueryFileByPage")
    @GetMapping("listFilesByPage")
    @ResponseBody
    public List<File> listFilesByPage(@RequestParam(required = false) Integer current,
            @RequestParam(required = false) Integer pageSize) {
        return fileService.listFilesByPage(current, pageSize);
    }

    /**
     * 分页查询文件列表.
     *
     * @param limit the limit
     * @param offset the offset
     * @return the list
     */
    @GetMapping("all")
    @ResponseBody
    public List<File> listFiles(@RequestParam(required = false, defaultValue = "200") Integer limit,
            @RequestParam(required = false, defaultValue = "0") Integer offset) {
        return fileService.listFiles(limit, offset);
    }

    /**
     * 获取文件信息.
     *
     * @param id the id
     * @return the file
     */
    @ApiOperation("Query file info")
    @GetMapping("{id}")
    @ResponseBody
    public File fileInfo(@PathVariable String id) {
        return fileService.getFileById(id).orElse(null);
    }

    /**
     * 获取文件数据.
     *
     * @param id the id
     * @param response the response
     */
    @ApiOperation("Get file data")
    @GetMapping("data/{id}")
    public void getFileData(@PathVariable String id, HttpServletResponse response) {
        fileService.getFileData(id, response);
    }

    /**
     * 上传接口.
     *
     * @param file MultipartFile文件对象
     * @return the file
     */
    @ApiOperation("Upload file")
    @PostMapping("/upload")
    @ResponseBody
    public File uploadFile(@RequestParam("file") MultipartFile file) {
        return fileService.uploadFile(file);
    }

    /**
     * 删除文件.
     *
     * @param id the id
     * @return the string
     */
    @ApiOperation("Delete file by id")
    @DeleteMapping("/{id}")
    @ResponseBody
    public String deleteFile(@PathVariable String id) {
        return fileService.deleteFile(id);
    }
}
